package com.chenyi.data.structure.stack;

/**
 * @author by chenyi
 * @className TestMain
 * @date 2021/12/27 15:38
 */

public class TestMain {
    public static void main(String[] args) {
        testReversePolishNotation();
    }

    public static void testArrayStack() {
        ArrayStack arrayStack = new ArrayStack(3);
        arrayStack.push(1);
        arrayStack.push(2);
        arrayStack.push(3);
        arrayStack.push(3);
        System.out.println(arrayStack.peekLast());
        System.out.println("==================");

        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.peekLast());
        System.out.println("=================");
        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.peekLast());
        System.out.println("=================");
        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.peekLast());
        System.out.println("=================");
        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.peekLast());


    }

    public static void testCalculatorStack() {
        String expression = "10000/2";
        // 数栈
        CalculatorStack<Integer> numStack = new CalculatorStack<>(20);
        // 符号栈
        CalculatorStack<Character> operatorStack = new CalculatorStack<>(20);
        char[] chars = expression.toCharArray();
        // 扫描整个表达式
        for (int i = 0; i < chars.length; i++) {
            // 判断是否为符号
            if (CalculatorStack.isOperator(chars[i])) {
                // 判断符号栈是否为空
                if (!operatorStack.isEmpty()) {
                    // 获取两个运算符的优先级
                    int peekSize = CalculatorStack.operatorSize(operatorStack.peekLast());
                    int thisSize = CalculatorStack.operatorSize(chars[i]);
                    // 判断运算符优先级
                    if (peekSize >= thisSize) {
                        // 如果大于等于当前运算符的优先级那么就先进行计算
                        Integer num1 = numStack.pop();
                        Integer num2 = numStack.pop();
                        Character operator = operatorStack.pop();
                        int result = CalculatorStack.calculate(num1, num2, operator);
                        // 当前结果存入数栈
                        numStack.push(result);
                    }
                }
                // 存储当前符号
                operatorStack.push(chars[i]);
            } else {
                StringBuilder num = new StringBuilder();
                num.append(chars[i]);
                // 继续查看下一位是否是数字
                for (int j = i + 1; j < chars.length; j++) {
                    if (!CalculatorStack.isOperator(chars[j])) {
                        num.append(chars[j] - 48);
                        i++;
                    } else {
                        break;
                    }
                }
                numStack.push(Integer.parseInt(num.toString()));
            }
        }
        // 从数栈和符号栈中分别pop出对应的符号与数字进行计算
        while (!operatorStack.isEmpty()) {
            Integer num1 = numStack.pop();
            Integer num2 = numStack.pop();
            Character operator = operatorStack.pop();
            int result = CalculatorStack.calculate(num1, num2, operator);
            numStack.push(result);
        }

        System.out.println(numStack.peekLast());
    }

    public static void testReversePolishNotation() {
        String expression = "10+((2+3)*4)-5";
        String reversePolishNotation = ReversePolishNotation.getReversePolishNotation(expression);
        System.out.println(reversePolishNotation);

        System.out.println("======================");

        System.out.println(ReversePolishNotation.reversePolishNotationCalculator(reversePolishNotation));
    }


}
